﻿using IdentityExample.Data.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity.Migrations;

namespace IdentityExample.Data
{
    public static class Seeder
    {
        public static void Seed(IdentityExample.Data.ApplicationDbContext context)
        {
            UserStore<ApplicationUser> userStore = new UserStore<ApplicationUser>(context);
            UserManager<ApplicationUser> userManager = new UserManager<ApplicationUser>(userStore);

            RoleStore<Role> roleStore = new RoleStore<Role>(context);
            RoleManager<Role> roleManager = new RoleManager<Role>(roleStore);


            ApplicationUser user1 = null;

            user1 = userManager.FindByName("nick@email.com");

            if (user1 == null)
            {
                userManager.Create(new ApplicationUser
                {
                    Email = "nick@email.com",
                    FirstName = "Nick",
                    LastName = "Brittain",
                    UserName = "nick@email.com"
                }, "123456");

                user1 = userManager.FindByName("nick@email.com");
            }

            if (!roleManager.Roles.Any())
            {
                roleManager.Create(new Role
                {
                    Name = "Admin"
                });

                roleManager.Create(new Role
                {
                    Name = "General"
                });
            }

            if (!userManager.IsInRole(user1.Id, "Admin"))
            {
                userManager.AddToRole(user1.Id, "Admin");
            }

            if (!context.Ratings.Any())
            {
                context.Ratings.Add(new Rating
                {
                    Name = "1 star",
                    CreatedByUserId = user1.Id,
                    UpdatedByUserId = user1.Id,
                    CreatedDate = DateTime.Now,
                    UpdatedDate = DateTime.Now
                });
                context.Ratings.Add(new Rating
                {
                    Name = "2 star",
                    CreatedByUserId = user1.Id,
                    UpdatedByUserId = user1.Id,
                    CreatedDate = DateTime.Now,
                    UpdatedDate = DateTime.Now
                });
                context.Ratings.Add(new Rating
                {
                    Name = "3 star",
                    CreatedByUserId = user1.Id,
                    UpdatedByUserId = user1.Id,
                    CreatedDate = DateTime.Now,
                    UpdatedDate = DateTime.Now
                });
                context.Ratings.Add(new Rating
                {
                    Name = "4 star",
                    CreatedByUserId = user1.Id,
                    UpdatedByUserId = user1.Id,
                    CreatedDate = DateTime.Now,
                    UpdatedDate = DateTime.Now
                });
                context.Ratings.Add(new Rating
                {
                    Name = "5 star",
                    CreatedByUserId = user1.Id,
                    UpdatedByUserId = user1.Id,
                    CreatedDate = DateTime.Now,
                    UpdatedDate = DateTime.Now
                });

                context.SaveChanges();
            }

            Movie movie1 = new Movie
            {
                Title = "LOTR",
                CreatedByUserId = user1.Id,
                UpdatedByUserId = user1.Id,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now
            };

            context.Movies.AddOrUpdate(m => m.Title, movie1);
            context.SaveChanges();

            UserMovie userMovie = context.UsersMovies.FirstOrDefault(u => u.UserId == user1.Id && u.MovieId == movie1.MovieId);

            if (userMovie == null)
            {
                userMovie = new UserMovie
                {
                    UserId = user1.Id,
                    MovieId = movie1.MovieId,
                    RatingId = context.Ratings.FirstOrDefault(r => r.Name == "4 star").RatingId,
                    CreatedByUserId = user1.Id,
                    UpdatedByUserId = user1.Id,
                    CreatedDate = DateTime.Now,
                    UpdatedDate = DateTime.Now
                };

                context.UsersMovies.Add(userMovie);
                context.SaveChanges();
            }
        }
    }
}
